package com.uneed.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import android.widget.Adapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import java.util.LinkedList;

import com.uneed.R;

public class LiteListView extends LinearLayout
  implements View.OnFocusChangeListener, View.OnClickListener, View.OnLongClickListener
{
	  private static final Object DIVIDER_TAG = new Object();
	  private Adapter adapter;
	  public boolean alwaysShowDivider;
	  private Handler handler = new Handler()
	  {
	    public void handleMessage(Message paramMessage)
	    {
	      if (paramMessage.what == 1)
	      {
	        LinkedList localLinkedList2 = null;
	        LinkedList localLinkedList1 = null;
	        
	        Object localObject = null;
	        int k = 0;
	        if ((adapter != null) && (!adapter.isEmpty()))
	        {
	          localLinkedList2 = new LinkedList();
	          localLinkedList1 = new LinkedList();
	          int j = Math.min(getChildCount(), -1 + 2 * adapter.getCount());
	          for (int i =0;i <j;i++)
	          {
	            localObject = LiteListView.this.getChildAt(i);
	            if (((View)localObject).getTag() != LiteListView.DIVIDER_TAG)
	              localLinkedList2.addLast(localObject);
	            else
	              localLinkedList1.addLast(localObject);
	          }
	          LiteListView.this.removeAllViews();
	          if (LiteListView.this.adapter instanceof BaseAdapter)
	            localObject = (BaseAdapter)adapter;
	          else
	            localObject = null;
	        }
	        boolean flag = false;
	        for (int j=0;j<adapter.getCount();j++)
	        {
	          if ((localObject == null) || (((BaseAdapter)localObject).isEnabled(j)))
	            flag = true;
	          else
	            flag = false;

	          View localView3;
			if (((alwaysShowDivider) && (j > 0)) || flag)
	         {
	            if (!localLinkedList1.isEmpty())
	              localView3 = (View)localLinkedList1.removeFirst();
	            else
	              localView3 = getDivider();
	            addView(localView3);
	          }
	          if (!localLinkedList2.isEmpty())
	            localView3 = (View)localLinkedList2.removeFirst();
	          else
	            localView3 = null;
	          
	          View localView2 = adapter.getView(j, localView3, LiteListView.this);
	          
	          if (false)
	          {
	            localView2.setOnFocusChangeListener(null);
	            localView2.setOnClickListener(null);
	            localView2.setOnLongClickListener(null);
	            localView2.setSelected(false);
	            localView2.setFocusable(false);
	            localView2.setClickable(false);
	            LiteListView.this.setBackground(localView2, false);
	            localView2.setLongClickable(false);
	          }
	          else
	          {
	            localView2.setOnFocusChangeListener(LiteListView.this);
	            localView2.setFocusable(true);
	            localView2.setClickable(true);
	            LiteListView.this.setBackground(localView2, true);
	            localView2.setOnClickListener(LiteListView.this);
	            if (LiteListView.this.onLongClickListener != null)
	            {
	              localView2.setOnLongClickListener(LiteListView.this);
	            }
	            else
	            {
	              localView2.setOnLongClickListener(null);
	              localView2.setLongClickable(false);
	            }
	          }
	          addView(localView2);
//	          localView2 = localView1;
//	          continue;
//	          LiteListView.this.removeAllViews();
	        }
	      }
	    }
	  };

  
  private LayoutInflater inflater;
 
  private final DataSetObserver observer = new DataSetObserver()
  {
  @Override
	  public void onChanged()
    {
      LiteListView.this.handler.removeMessages(1);
      LiteListView.this.handler.sendEmptyMessageDelayed(1, 100L);
    }

  @Override
  public void onInvalidated()
    {
      onChanged();
    }
  };

  private AdapterView.OnItemClickListener onClickListener;
  private AdapterView.OnItemLongClickListener onLongClickListener;

  public LiteListView(Context paramContext)
  {
    this(paramContext, null);
  }

  public LiteListView(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    TypedArray localTypedArray = paramContext.obtainStyledAttributes(paramAttributeSet, R.styleable.LiteListView);
    this.alwaysShowDivider = localTypedArray.getBoolean(0, false);
    localTypedArray.recycle();
    this.inflater = LayoutInflater.from(paramContext);
    setOrientation(1);
  }


  public Adapter getAdapter()
  {
    return this.adapter;
  }

  
  protected View getDivider()
  {
    View localView = this.inflater.inflate(R.layout.list_divider, this, false);
    localView.setTag(DIVIDER_TAG);
    return localView;
  }

  @Override
  public void onFocusChange(View paramView, boolean paramBoolean)
  {
    paramView.setSelected(paramBoolean);
  }

  
  public void setAdapter(Adapter paramAdapter)
  {
    if (this.adapter != null)
      this.adapter.unregisterDataSetObserver(this.observer);
    this.adapter = paramAdapter;
    if (this.adapter != null)
      this.adapter.registerDataSetObserver(this.observer);
    removeAllViews();
    this.observer.onChanged();
  }

  
  public void setOnItemClickListener(AdapterView.OnItemClickListener paramOnItemClickListener)
  {
    this.onClickListener = paramOnItemClickListener;
    this.handler.sendEmptyMessage(1);
  }

  public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener paramOnItemLongClickListener)
  {
    this.onLongClickListener = paramOnItemLongClickListener;
    this.handler.sendEmptyMessage(1);
  }
  
  private void setBackground(View paramView, boolean paramBoolean)
  {
    if (paramBoolean)
    {
      int i = paramView.getPaddingLeft();
      int m = paramView.getPaddingRight();
      int j = paramView.getPaddingTop();
      int k = paramView.getPaddingBottom();
      paramView.setBackgroundResource(R.drawable.list_item);
      paramView.setPadding(i, j, m, k);
    }
  }

 @Override
public void onClick(View paramView) {
	// TODO Auto-generated method stub
    if (this.onClickListener != null)
    {
      for(int i=0;i<getChildCount();i++) {
//          if (getChildAt(i).getTag() == DIVIDER_TAG) return;
          if ((getChildAt(i) == paramView)&& (getChildAt(i).getTag() != DIVIDER_TAG)) {
              
          if (this.adapter != null)
        	  this.onClickListener.onItemClick(null, paramView, i, adapter.getItemId(i));
          }	  
      }
    }
}

 @Override
public boolean onLongClick(View v) {
	// TODO Auto-generated method stub
	return false;
}

}
